//
//  BilibiliVideoDataV1.swift
//  TestConcurreces
//
//  Created by rolodestar on 2025/6/16.
//
import SwiftData
import Foundation
import BilibiliSearch
import RoSwiftDataForNetVideoDownload
typealias BilibiliNetVideo = BilibiliNetVideoV3
@Model
class BilibiliNetVideoV2: NetVideoProtol{
    var id: UUID
    
    var remoteUrl: URL
    
    var downloadProgress: Double
    
    var resumeData: Data?
    
    var localUrl: URL?
    
   
    var title: String
    var  playUrl: URL // play_url 驼峰命名
    var url: URL
    var cover: URL // 封面 URL
    var searchKeyword: String?
//    var tips: String
    var time: Date // 自动解析为 Date 类型
    
    
  
    var author: String
   
    var duration: String // 时长字符串（可按需转换为 TimeInterval）
    var videoDescription: String // 视频描述（注意转义字符自动处理）
    var playCount: Int // JSON 中为 play_count，通过 CodingKeys 映射
    var pubdate: Date // 发布时间（自动解析为 Date 类型）
    
    init(id: UUID, remoteUrl: URL, downloadProgress: Double, resumeData: Data? = nil, localUrl: URL? = nil, title: String, playUrl: URL, url: URL, cover: URL, searchKeyword: String? = nil,  time: Date, author: String, duration: String, videoDescription: String, playCount: Int, pubdate: Date) {
        self.id = id
        self.remoteUrl = remoteUrl
        self.downloadProgress = downloadProgress
        self.resumeData = resumeData
        self.localUrl = localUrl
        self.title = title
        self.playUrl = playUrl
        self.url = url
        self.cover = cover
        self.searchKeyword = searchKeyword
        self.time = time
        self.author = author
        self.duration = duration
        self.videoDescription = videoDescription
        self.playCount = playCount
        self.pubdate = pubdate
    }

    static func createModel(from result: BilibiliSearchItemParsingResult) -> BilibiliNetVideo{
        return BilibiliNetVideo(id: UUID(),
                                remoteUrl: result.data.playUrl,
                                downloadProgress: 0.0,
                                title: result.data.title,
                                playUrl: result.data.playUrl,
                                url: result.data.url,
                                cover: result.data.cover,
                                time: result.time,
                                author: result.biliSearchItemResult?.data.author ?? "未知作者",
                                duration: result.biliSearchItemResult?.data.duration ?? "0:0",
                                videoDescription: result.biliSearchItemResult?.data.description ?? "没有描述",
                                playCount: result.biliSearchItemResult?.data.playCount ?? 0,
                                pubdate: result.biliSearchItemResult?.data.pubdate ?? Date.now
        )
    }
}

@Model
class BilibiliNetVideoV3: NetVideoProtol{
    var id: UUID
    
    var remoteUrl: URL
    
    var downloadProgress: Double
    
    var resumeData: Data?
    
    var localUrl: URL?
    
   
    var title: String
    var  playUrl: URL // play_url 驼峰命名
    var url: URL
    var cover: URL // 封面 URL
    var searchKeyword: String?
//    var tips: String
    var time: Date // 自动解析为 Date 类型
    
    
  
    var author: String
   
    var duration: String // 时长字符串（可按需转换为 TimeInterval）
    var videoDescription: String // 视频描述（注意转义字符自动处理）
    var playCount: Int // JSON 中为 play_count，通过 CodingKeys 映射
    var pubdate: Date // 发布时间（自动解析为 Date 类型）
    
    init(id: UUID, remoteUrl: URL, downloadProgress: Double, resumeData: Data? = nil, localUrl: URL? = nil, title: String, playUrl: URL, url: URL, cover: URL, searchKeyword: String? = nil,  time: Date, author: String, duration: String, videoDescription: String, playCount: Int, pubdate: Date) {
        self.id = id
        self.remoteUrl = remoteUrl
        self.downloadProgress = downloadProgress
        self.resumeData = resumeData
        self.localUrl = localUrl
        self.title = title
        self.playUrl = playUrl
        self.url = url
        self.cover = cover
        self.searchKeyword = searchKeyword
        self.time = time
        self.author = author
        self.duration = duration
        self.videoDescription = videoDescription
        self.playCount = playCount
        self.pubdate = pubdate
    }

    static func createModel(from result: BilibiliSearchItemParsingResult) -> BilibiliNetVideo{
        return BilibiliNetVideo(id: UUID(),
                                remoteUrl: result.data.playUrl,
                                downloadProgress: 0.0,
                                title: result.data.title,
                                playUrl: result.data.playUrl,
                                url: result.data.url,
                                cover: result.data.cover,
                                time: result.time,
                                author: result.biliSearchItemResult?.data.author ?? "未知作者",
                                duration: result.biliSearchItemResult?.data.duration ?? "0:0",
                                videoDescription: result.biliSearchItemResult?.data.description ?? "没有描述",
                                playCount: result.biliSearchItemResult?.data.playCount ?? 0,
                                pubdate: result.biliSearchItemResult?.data.pubdate ?? Date.now
        )
    }
}
